﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Configuration;

namespace Common
{
    public class EMailUtility
    {
        private string _MailHost;
        /// <summary>
        /// 主机
        /// </summary>
        public string MailHost
        {
            get { return _MailHost; }
            set { _MailHost = value; }
        }

        private int _MailPort;
        /// <summary>
        /// 主机端口
        /// </summary>
        public int MailPort
        {
            get { return _MailPort; }
            set { _MailPort = value; }
        }

        private string _MailAccount;
        /// <summary>
        /// 用户ID
        /// </summary>
        public string MailAccount
        {
            get { return _MailAccount; }
            set { _MailAccount = value; }
        }


        private string _MailPW;
        /// <summary>
        /// 用户密码
        /// </summary>
        public string MailPW
        {
            get { return _MailPW; }
            set { _MailPW = value; }
        }

        private string _MailTitle;
        /// <summary>
        /// 用户显示名称
        /// </summary>
        public string MailTitle
        {
            get { return _MailTitle; }
            set { _MailTitle = value; }
        }

        /// <summary>
        /// smtp客户端
        /// </summary>
        private SmtpClient _smtpClient;

        /// <summary>
        /// 邮件类
        /// </summary>
        private MailMessage _mailMessage;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_hostUrl">smtp服务器地址，例：smtp.163.com</param>
        /// <param name="_hostPort">smtp端口地址</param>
        /// <param name="_account">发送邮箱账号，例a@163.com</param>
        /// <param name="_accountPassword">发送邮箱密码</param>
        /// <param name="_sendTitle">发送者名</param>
        public EMailUtility(string _hostUrl = null, int _hostPort = 0, string _account = null, string _accountPassword = null, string _sendTitle = null)
        {
            _MailHost = _hostUrl;
            _MailPort = _hostPort;
            _MailAccount = _account;
            _MailPW = _accountPassword;
            _MailTitle = _sendTitle;

            _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            _smtpClient.EnableSsl = false;
            _smtpClient.Host = MailHost;
            _smtpClient.Port = MailPort;
            _smtpClient.UseDefaultCredentials = true;
            _smtpClient.Credentials = new NetworkCredential(MailAccount, MailPW);

            _mailMessage = new MailMessage();
            _mailMessage.Priority = MailPriority.Normal;
            _mailMessage.From = new MailAddress(MailAccount, _sendTitle, Encoding.GetEncoding(936));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myInfo">邮件信息</param>
        /// <param name="ToList">收件列表</param>
        /// <param name="myAttFiles">附件列表</param>
        /// <param name="ToCC">抄送列表</param>
        /// <param name="ToBcc">密送列表</param>
        /// <returns></returns>
        public string SendMail(string myInfo, IList<string> ToList, string FromName, string mySubject, IList<string> myAttFiles = null, IList<string> ToCC = null, IList<string> ToBcc = null)
        {
            string myError = null;

            if (ToList != null && ToList.Count > 0)
            {
                try
                {
                    _mailMessage.IsBodyHtml = true;
                    _mailMessage.BodyEncoding = Encoding.GetEncoding(936);

                    if (ToList != null && ToList.Count > 0)
                    {
                        //MyMail.To.Add(CommonFunction.GetStringByList(ToList, ','));
                    }

                    if (ToCC != null && ToCC.Count > 0)
                    {
                        //MyMail.CC.Add(CommonFunction.GetStringByList(ToCC, ','));
                    }

                    if (ToBcc != null && ToBcc.Count > 0)
                    {
                       // MyMail.Bcc.Add(CommonFunction.GetStringByList(ToBcc, ','));
                    }

                    _mailMessage.From = new MailAddress(MailAccount, FromName);

                    _mailMessage.Subject = mySubject;

                    _mailMessage.Body = myInfo;

                    if (myAttFiles != null && myAttFiles.Count > 0)
                    {
                        foreach (string myItem in myAttFiles)
                        {
                            _mailMessage.Attachments.Add(new Attachment(myItem));
                        }
                    }
                    _smtpClient.Send(_mailMessage);
                }
                catch (Exception ex)
                {
                    myError = "发送失败！原因：" + ex.Message;
                }
            }
            else
            {
                myError = "发送列表为空！";
            }

            return myError;
        }


    }
}
